package com.zmn.oms.dubbo.impl.zmn.master.part;

import com.alibaba.fastjson.JSON;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.oms.business.interfaces.part.OrderPartBService;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.common.dro.part.CheckOrderPartIndicatorsDIO;
import com.zmn.oms.common.dro.part.OrderPartDIO;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.dubbo.dto.common.work.OrderPartDRO;
import com.zmn.oms.dubbo.impl.OmsBaseRemoteService;
import com.zmn.oms.dubbo.interfaces.zmn.master.part.PartListRemoteService;
import com.zmn.oms.dubbo.utils.DubboConsts;
import com.zmn.oms.model.dto.part.CheckOrderPartIndicatorsDTO;
import com.zmn.oms.model.entity.part.OrderPart;
import com.zmn.oms.services.interfaces.part.OrderPartService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * 类描述: 配件查询
 *
 * @author: liudi
 * @since: 2022/03/22 16:45
 */
@Slf4j
@DubboService(loadbalance = "roundrobin", timeout = 20000, retries = 2, version = DubboConsts.INTERFACE_VERSION)
public class PartListRemoteServiceImpl extends OmsBaseRemoteService implements PartListRemoteService {

	@Autowired
	private OrderPartService orderPartService;
	@Autowired
	private OrderPartBService orderPartBService;

	/**
	 * 根据订单ID查询出内采配件
	 *
	 * @param orderId
	 * @return
	 */
	@Override
	public ResponseDTO<List<OrderPartDRO>> listInSourcingPartByOrderId(Long orderId) {
		log.info("PartListRemoteServiceImpl#listInSourcingPartByOrderId 入参 orderId：{}", orderId);
		List<OrderPart> orderParts = orderPartService.listByOrderId(orderId, OrderConsts.PART_TYPE_SOURCING_IN);
		log.info("PartListRemoteServiceImpl#listInSourcingPartByOrderId 出参 orderId：{}", JSON.toJSONString(orderParts));
		List<OrderPartDRO> orderPartDROList = BeanMapper.mapList(orderParts, OrderPartDRO.class);
		return ResponseDTO.success(orderPartDROList);
	}

	@Override
	public ResponseDTO checkOrderGoodsIndicators(CheckOrderPartIndicatorsDIO checkOrderPartIndicatorsDIO) {
		log.info("checkOrderGoodsIndicators dio={}", checkOrderPartIndicatorsDIO);
		CheckOrderPartIndicatorsDTO dto = new CheckOrderPartIndicatorsDTO();
		dto.setOrderId(checkOrderPartIndicatorsDIO.getOrderId());
		dto.setWorkId(checkOrderPartIndicatorsDIO.getWorId());
		dto.setPartType(checkOrderPartIndicatorsDIO.getPartType());
		List<OrderPart> orderParts = new ArrayList<>(checkOrderPartIndicatorsDIO.getPartDIOList().size());
		for (OrderPartDIO orderPartDIO : checkOrderPartIndicatorsDIO.getPartDIOList()) {
			OrderPart orderPart = new OrderPart();
			orderPart.setPrice(orderPartDIO.getPrice());
			orderPart.setNumber(orderPartDIO.getNum());
			orderPart.setPartId(orderPartDIO.getPartId());
			orderParts.add(orderPart);
		}
		dto.setOrderPartList(orderParts);
		dto.setOperatorType(GlobalConsts.OPERATE_USER_TYPE_MASTER);

		try {
			orderPartBService.checkOrderGoodsIndicators(dto);
		} catch (OmsBaseException e) {
			return ResponseDTO.fail(e.getCode(), e.getMessage());
		}
		return ResponseDTO.success();
	}
}
